package cm.just;

import com.alibaba.fastjson.JSON;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wujiaming
 */
@WebServlet("/ai")
public class AiServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        String sentence = request.getParameter("sentence");
        System.out.println(sentence);
        response.setContentType("text/html;charset=utf-8");

        sentence = sentence.replace("吗", "");
        sentence = sentence.replace("我", "我也");
        sentence = sentence.replace("?", "！");
        System.out.println(sentence);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("user", sentence);// 表示成功状态
        // jsonString 表示格式转化后的Json数据。
        String jsonString = JSON.toJSONString(resultMap);
        System.out.println(jsonString);
        // 把这个格式化后的json数据传递给页面
        response.setContentType("application/json;charset=utf-8");
        // 写数据到resp里面
		PrintWriter writer = response.getWriter();
		// 写操作
		writer.write(jsonString);
		// 清空缓存
		writer.flush();
		// 写关闭
		writer.close();
    }
}
